iT邦幫忙

2024 iThome 鐵人賽

DAY 6
0
Software Development

LSTM結合Yolo v8對於多隻斑馬魚行為分析系列 第 6

day 6 lstm結合yolo斑馬魚行為分析

  • 分享至 

  • xImage
  •  

今天是第六天可以寫一個lstm結合yolo的程式,以下是程式碼
Yolo部分

import cv2
from yolo_model import YOLO  # 假設這是YOLO模型的導入

class ZebrafishDetector:
    def __init__(self, yolo_weights_path):
        self.yolo = YOLO(yolo_weights_path)

    def detect_fish(self, frame):
        detected_boxes, detected_scores, detected_classes = self.yolo.detect(frame)
        if detected_boxes:
            box = detected_boxes[0]  # 假設我們只處理第一個偵測到的斑馬魚
            x, y, w, h = box
            fish_frame = frame[y:y+h, x:x+w]
            return fish_frame, box
        return None, None

# 主程式入口(YOLO測試)
if __name__ == "__main__":
    video_path = 'zebrafish_video.mp4'
    detector = ZebrafishDetector('yolo_weights.h5')
    cap = cv2.VideoCapture(video_path)

    while True:
        ret, frame = cap.read()
        if not ret:
            break
        
        fish_frame, box = detector.detect_fish(frame)
        if fish_frame is not None:
            x, y, w, h = box
            cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2)

        cv2.imshow("YOLO Zebrafish Detection", frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

    cap.release()
    cv2.destroyAllWindows()

Lstm部分

import numpy as np
import tensorflow as tf
from lstm_model import LSTMModel  # 假設這是LSTM模型的導入

class BehaviorPredictor:
    def __init__(self, lstm_weights_path):
        self.lstm_model = LSTMModel()
        self.lstm_model.load_weights(lstm_weights_path)

    def predict_behavior(self, frame_sequence):
        input_sequence = np.array(frame_sequence)
        input_sequence = np.expand_dims(input_sequence, axis=0)  # LSTM需要4D的輸入格式 (batch_size, time_steps, height, width, channels)
        behavior_prediction = self.lstm_model.predict(input_sequence)
        predicted_behavior = np.argmax(behavior_prediction)
        return predicted_behavior

# 主程式入口(LSTM測試)
if __name__ == "__main__":
    # 這裡假設我們有一些斑馬魚的影像序列
    frame_sequence = []  # 應該包含一段時間內的斑馬魚影像

    predictor = BehaviorPredictor('lstm_weights.h5')
    predicted_behavior = predictor.predict_behavior(frame_sequence)
    print(f"Predicted Behavior: {predicted_behavior}")

合起來

import cv2
from yolo_module import ZebrafishDetector  # 假設這是你保存YOLO模型的模組
from lstm_module import BehaviorPredictor  # 假設這是你保存LSTM模型的模組

# 初始化YOLO和LSTM模型
detector = ZebrafishDetector('yolo_weights.h5')
predictor = BehaviorPredictor('lstm_weights.h5')

def analyze_zebrafish_behavior(video_path):
    cap = cv2.VideoCapture(video_path)
    if not cap.isOpened():
        print("Error: Cannot open video.")
        return

    frame_buffer = []
    while True:
        ret, frame = cap.read()
        if not ret:
            break
        
        # YOLO 物件偵測
        fish_frame, box = detector.detect_fish(frame)
        if fish_frame is not None:
            frame_buffer.append(fish_frame)

            if len(frame_buffer) >= 10:  # 假設用最近的10幀來進行行為分析
                predicted_behavior = predictor.predict_behavior(frame_buffer[-10:])
                cv2.putText(frame, f"Predicted Behavior: {predicted_behavior}", 
                            (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 2)

        # 顯示處理後的幀
        cv2.imshow("Zebrafish Behavior Analysis", frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

    cap.release()
    cv2.destroyAllWindows()

# 執行分析
analyze_zebrafish_behavior('zebrafish_video.mp4')

這三段程式碼將 YOLO 和 LSTM 模型分別處理斑馬魚的偵測與行為預測,並且封裝成模組化的結構。讓我逐步解釋每一部分的作用及其運作方式。

1. YOLO 模型部分

這部分的程式碼負責使用 YOLO 模型來偵測影像中的斑馬魚位置。

  • ZebrafishDetector 類:
    • 這個類是用來偵測斑馬魚的工具。
    • 當你建立這個類的實例時,會傳入一個 YOLO 模型的權重檔案,這樣它就可以利用 YOLO 模型來偵測斑馬魚。
    • detect_fish 方法:
      • 這個方法會接收一張影像(frame),然後用 YOLO 模型來檢測是否有斑馬魚在影像中。
      • 如果偵測到斑馬魚,它會返回斑馬魚所在的影像區域(裁剪後的影像)以及斑馬魚的邊界框位置。如果沒有偵測到斑馬魚,則返回 None
  • 主程式(YOLO 測試):
    • 這裡我們會讀取一段影片,並逐幀處理影像。
    • 每幀影像都會使用 YOLO 模型來偵測斑馬魚的位置,並將偵測到的斑馬魚框選出來,顯示在螢幕上。
    • 如果按下 q 鍵,可以停止播放影片。

2. LSTM 模型部分

這部分的程式碼用來根據斑馬魚的行為影像序列進行行為預測。

  • BehaviorPredictor 類:
    • 這個類是用來預測斑馬魚行為的工具。
    • 當你建立這個類的實例時,會傳入一個 LSTM 模型的權重檔案,這樣它就可以利用 LSTM 模型來分析行為。
    • predict_behavior 方法:
      • 這個方法會接收一個影像序列(通常是多幀影像,表示一段時間內的行為),然後使用 LSTM 模型進行行為預測。
      • 最後返回一個預測結果,這個結果表示斑馬魚最有可能的行為,比如焦慮、害怕等。
  • 主程式(LSTM 測試):
    • 這裡假設已經有一組斑馬魚的影像序列,這些影像可以是 YOLO 偵測出來的部分。
    • 我們用 LSTM 模型來分析這些影像,並輸出一個行為預測結果。

3. 主程式

這部分程式碼將 YOLO 和 LSTM 模型結合在一起進行斑馬魚行為分析。

  • 初始化 YOLO 和 LSTM 模型:

    • 我們先建立 ZebrafishDetector(YOLO 模型)和 BehaviorPredictor(LSTM 模型)的實例。
  • analyze_zebrafish_behavior 函數:

    • 這個函數負責讀取影片,並使用 YOLO 和 LSTM 模型來分析斑馬魚行為。
    • 每幀影像會先使用 YOLO 模型來偵測斑馬魚的位置。如果找到了斑馬魚,這幀影像會被存入一個緩衝區中。
    • 當累積足夠多的影像(例如10幀),我們會把這些影像傳遞給 LSTM 模型進行行為分析,並將結果顯示在螢幕上。
    • 影片會逐幀播放,你可以邊看影片邊看到即時的行為分析結果。

總結:

  • YOLO 模型部分負責找出影片中的斑馬魚並標記它們的位置。
  • LSTM 模型部分負責分析斑馬魚的行為,基於一段時間內的影像序列來預測它可能的行為。
  • 主程式將這兩個部分結合,實現即時的斑馬魚行為分析。

這樣的模組化設計使得程式更易於維護和擴展。你可以輕鬆地修改或替換某個模組,而不會影響整個系統的運作。


上一篇
Day 5 yolo多隻斑馬魚行為分析
下一篇
day 7 yolo v8介紹
系列文
LSTM結合Yolo v8對於多隻斑馬魚行為分析29
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言